//
//  CollectionVC.m
//  LoveTourism
//
//  Created by qianfeng0 on 16/6/24.
//  Copyright © 2016年 王超. All rights reserved.
//

#import "CollectionVC.h"
#import "FunModel.h"
#import "FunCell.h"
#import "FunCollectionManager.h"
#import "WebVC.h"
#import "FoodModel.h"
#import "FoodCell.h"
#import "VideoPlayViewController.h"
@interface CollectionVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) NSMutableArray * dataArray;

@property (nonatomic ,copy) NSString *flag;

@end

@implementation CollectionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.tableView.tableFooterView = [[UIView alloc] init];
    
}
#pragma mark - 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

#pragma mark - 初始化好吃的数据
- (void)getData {
    [self.tableView registerNib:[UINib nibWithNibName:@"FoodCell" bundle:nil] forCellReuseIdentifier:@"FoodCell"];
    [self.dataArray removeAllObjects];
    NSArray *ary = [NSArray new];
    ary = [[FunCollectionManager shareManager] getAllFavmodel];
    for (NSData *data in ary) {
        NSError *error = nil;
        FoodModel *model = [[FoodModel alloc] initWithData:data error:&error];
        [self.dataArray addObject:model];
    }
    _flag = @"1";
}

- (void)initUI {
    
    self.dataArray = [[FunCollectionManager shareManager] selectAll];
    
    //注册cell
    UINib * nib = [UINib nibWithNibName:@"FunCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"FunCell"];
}
#pragma mark - 按钮响应方法
- (IBAction)change:(UISegmentedControl *)sender {
    if (sender.selectedSegmentIndex == 0) {
        [self getData];
        [self.tableView reloadData];
        NSLog(@"好吃的");
        
    }else{
        _flag = @"0";
        [self initUI];
        [_tableView reloadData];
    }
    
}
- (IBAction)pop:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - tableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_flag isEqualToString:@"0"]) {
        //1.
        static NSString * identifier = @"FunCell";
        //2.
        FunCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        //赋值
        cell.model = self.dataArray[indexPath.row];
        
        //4.
        return cell;
    } else {
        FoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FoodCell"];
        cell.flag = _flag;
        cell.model = self.dataArray[indexPath.row];
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_flag isEqualToString:@"0"]) {
        return 180;
    } else {
        return 145;
    }
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //实现给cell添加一个3D动画，
    //设置x,y的初始值为0.1，z的初始值为1
    //一般的动画效果都是在layer层实现的
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    [UIView animateWithDuration:0.5 animations:^{
        //设置cell 的x和y的最终值为1
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
}
#pragma mark - delegate方法
//删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        if ([_flag isEqualToString:@"0"]) {
            //1.删除数据库中对应的数据
            [[FunCollectionManager shareManager] deleByName:self.dataArray[indexPath.row]];
            //2.删除tableView中的dataArray中的数据
            [self.dataArray removeObjectAtIndex:indexPath.row];
            //刷新表格
            //[self.tableView reloadData];
            
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        } else {
            [[FunCollectionManager shareManager] deleteFormFavBy:[self.dataArray[indexPath.row] dishes_id]];
            [self.dataArray removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_flag isEqualToString:@"0"]) {
        FunModel * model = self.dataArray[indexPath.row];
        WebVC * vc = [[WebVC alloc] initWithNibName:@"WebVC" bundle:nil];
        
        vc.str = model.eventDetailUrl;
        vc.model = model;
        
        //隐藏分栏
        vc.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        FoodModel *model = self.dataArray[indexPath.row];
        VideoPlayViewController *videoVC = [[VideoPlayViewController alloc] init];
        AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:model.video]];
        videoVC.player = player;
        [self presentViewController:videoVC animated:YES completion:nil];
    }
    
    
}

@end
